import React from "react";
import BaseComponent from "../../../redare/components/BaseComponent";
import ResizeGrid from "../../../redare/components/grid/ResizeGrid";
import {Divider} from "antd";
import ModalNoAnt from "../../common/ModalNoAnt";
import DepartmentForm from "./DepartmentForm";
import DictionaryPresenter from "../../../.gen/presenter/DictionaryPresenter";

export default class DepartmentGrid extends BaseComponent {

    constructor(props) {
        super(props);
        this.presenter = new DictionaryPresenter(this)
    }

    componentDidMount() {
        this._findDingDepartmentPage()
    }

    _findDingDepartmentPage(searchParams, page) {
        this.showGridLoading(true);
        this.searchParams = searchParams;
        if (!this.searchParams) {
            this.searchParams = {}
        }
        if (page) {
            this.curPage = page;
        }
        this.presenter.findDingDepartmentPage(
            {...this.searchParams, showParentName: true},
            {curPage: this.curPage,pageSize: 20},
            (result) =>{
                let {data} = result;
                if (data && this.grid) {
                    let {result, curPage, totalCount, pageSize} = data;
                    this.curPage = curPage;
                    this.grid.setData(result, {curPage, pageSize, totalCount})
                }
            },
            () => {
                this.showGridLoading(false)
            }
        )
    }

    _edit(record) {
        this.editId = record.id;
        this.departmentModal.show();
        setTimeout(() =>{
            let form = this.departmentModal.getInstance();
            if (form) {
                form.setFormData(record)
            }
        }, 300)
    }

    _editDepartment(callback) {
        let formInstance = this.departmentModal.getInstance();
        formInstance.validateFormFields()
            .then(values => {
                callback.openLoading();
                values.id = this.editId;
                this.presenter.editDingDepartment(
                    values,
                    {},
                    () => {
                        this.toastSuccess("编辑成功");
                        this._findDingDepartmentPage();
                        callback.closeModal()
                    },
                    () => {
                        callback.closeLoading()
                    }
                )
            })
    }

    _del(id) {
        this.confirm('确定删除该部门吗？', () =>{
            this.presenter.deleteDingDepartment(
                {id},
                {},
                () => {
                    this.toastSuccess("删除成功");
                    this._findDingDepartmentPage()
                }
            )
        })
    }

    onPageChange(curPage) {
        this.curPage = curPage;
        this._findDingDepartmentPage()
    }

    render() {
        return (
            <>
                <ResizeGrid
                    ref={(o) => {
                        this.grid = o
                    }}
                    size="middle"
                    bordered={false}
                    className={"scroll-table no-border-table"}
                    scroll={{y: 'calc(100vh - 380px)'}}
                    _initLoading={false}
                    _onPageChange={(page) => {
                        this.onPageChange(page.curPage);
                    }}
                    rowKey={(v) => v.id}
                    columns={[
                        {
                            title: '序号',
                            dataIndex: 'id',
                            width: 80,
                            align: 'center',
                            render: (text, record, index) => {
                                return (<span>{index + 1}</span>)
                            }
                        },
                        {
                            title: '部门编号',
                            width: 200,
                            dataIndex: 'departmentId'
                        },
                        {
                            title: '部门',
                            width: 200,
                            dataIndex: 'departmentName'
                        },
                        {
                            title: '上级部门',
                            width: 200,
                            dataIndex: 'departmentParentName'
                        },
                        {
                            title: '操作',
                            width: 120,
                            render: (value, record) => {
                                if (!record.isSystem) {
                                    return '-';
                                }
                                return (
                                    <>
                                        <a onClick={this._edit.bind(this, record)}>编辑</a>
                                        <Divider type="vertical"/>
                                        <a onClick={() => this._del(record.id)}>删除</a>
                                    </>
                                );
                            }
                        }
                    ]}
                />

                <ModalNoAnt
                    title="编辑部门"
                    destroyOnClose={true}
                    ref={o => this.departmentModal = o}
                    okfn={(callback) => this._editDepartment(callback)}
                >
                    <DepartmentForm parentName={this.state.title}/>
                </ModalNoAnt>
            </>
        );
    }

    showGridLoading(bool) {
        if (this.grid) {
            bool ? this.grid.showLoadingIndicator() : this.grid.closeLoadingIndicator()
        }
    }

}